package main.fileiterators;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.JFileChooser;
import javax.swing.UIManager;

import logic.Algorithm;
import logic.ConfigurationAlgorithm;
import memorydata.MemoryDataMaps;
import data.AlgorithmInput;
import data.ConfigurationInput;
import data.Data;
import data.GraphData;
import data.Saver;
import data.GeneralDefinitions.OrderingParameters;

public class CsvFileIterator {

	static final double price = Double.MAX_VALUE;
	static final double performence = Double.MAX_VALUE;
	static final double powerConsump = Double.MAX_VALUE;
	static final OrderingParameters priorityParam = OrderingParameters.kbRead;
	static final Calendar startDate = new GregorianCalendar(1990, 1, 12);
	static final Calendar endDate = GregorianCalendar.getInstance();

	public static void main(String[] args){
		try{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}catch(Exception e){
			e.printStackTrace();
		}

		JFileChooser fc = new JFileChooser();
		fc.setCurrentDirectory(new File(System.getProperty("user.dir")));
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int pick = fc.showOpenDialog(null);
		File f = fc.getSelectedFile();
		if (pick == JFileChooser.APPROVE_OPTION && f.isDirectory()){
			File saveDir = new File(f.getParent() +File.separator + "ini-files");
			saveDir.mkdir();
			File[] content = f.listFiles();
			for (int i=0; i < content.length; i++){
				if (content[i].isFile()) {
				} else if (content[i].isDirectory()){
					File[] dirContent = content[i].listFiles();
					File dirSaveDir = new File(saveDir.getAbsolutePath() + File.separator + content[i].getName());
					dirSaveDir.mkdir();
					for (int j=0; j < dirContent.length; j++){
						computeFileConfiguration(dirContent[j], dirSaveDir);
					}
				}
				computeFileConfiguration(content[i], saveDir);
			}
		} else {
			System.out.println("No directory chosen, Exiting");
		}
		logger.close();
	}

	private static void computeFileConfiguration(File f, File saveDir){
		System.out.println("Computing file - " + f.getAbsolutePath());
		if (f.isDirectory()) return;
		String tmpname = f.getName();
		String extension = tmpname.substring(tmpname.lastIndexOf('.'));
		String filename = tmpname.substring(0, tmpname.lastIndexOf('.'));
		if (extension.equalsIgnoreCase(".csv")){
			try{
				Data data = new MemoryDataMaps();
				data.parse(f.getAbsolutePath());
				AlgorithmInput in = new ConfigurationInput(price, performence, powerConsump, startDate, endDate, priorityParam);
				Algorithm algo = new ConfigurationAlgorithm();
				//		System.out.println(" @@@@@@@@ data = "+_data);
				GraphData out = algo.compute(in, data);
				Saver s = new Saver(saveDir.getAbsolutePath() + File.separator + filename);
				s.save(in, out);
			}catch (Exception e) {
				log("Exception when trying to parse file - " + tmpname);
				log("Message was: " + e.getMessage());
				log("");
			}
		}
	}

	static{
		File f = new File("log.txt");
		f.delete();

		try {
			f.createNewFile();
			logger = new PrintWriter(f);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	static PrintWriter logger;

	private static void log(String line){
		logger.println(line);
	}
}
